#ifndef DOWNLOAD_OPERATOR_H_
#define DOWNLOAD_OPERATOR_H_

#include "cx_system_define.h"
#include "comm_objbase.h"
#include "cx_system_define.h"
#include "cx_socket.h"
#include "cx_http.h"

/*************************************************************************
* INTERFACE
*************************************************************************/

enum QDownOperatorStatus
{
	EStartDownload,
	EUnknownError,
	EFinishOne,
	EFinishDownload
};

//#undef	THIS_HTTP_CTX
//#define THIS_HTTP_CTX( __m )		_qqr_http_ctx->__m

COMM_INTERFACE_( IDownOperatorObserver, 1, ICommO )
{
	__COMM_INHERIT_FROM( ICommO )

	COMM_METHOD( UpdateStatus)	( COMM_REF(ICommO) thiz, IN int status );
};

#define IComm_cx_DownOperatorObserver_VTBL( p )				COMM_GET_VTBL_(p, IDownOperatorObserver)
#define IComm_cx_DownOperatorObserver_CALL( p, a )				IComm_cx_DownOperatorObserver_VTBL( p )->a
#define IComm_cx_DownOperatorObserver_UpdateStatus( p, a)	    IComm_cx_DownOperatorObserver_CALL( p, UpdateStatus )( p, a )

enum OPERATORREQUESTTYPE{
	REQUEST_START = 0,
	REQUEST_STARTCURRENT,
	REQUEST_STARTCURRENTCHAPTER
};

typedef struct __struct_download_operator_data
{
	unsigned long		      ref_cnt;

	unsigned int              downloadid;
	unsigned short*           url;
	unsigned short*           actualurl;
	unsigned short*           filename;
	unsigned short*           operatorname;
	unsigned short*           resname;
	unsigned short*           operatorid;
	unsigned short            resid;
	unsigned short            maxresid;
	unsigned long             offset;
	unsigned char             dirty;
	unsigned long             receivenumber;
	unsigned long             onereceivenumber;
	unsigned long             totalnumber;

	unsigned short            downstatus;
	COMM_REF(ICommO)	      downtask;
	COMM_REF(ICommO)	      observer;
	COMM_REF(ICommO)	      downmanager;
	qqr_http_context_ptr_t _qqr_http_ctx;	
	CXIAP *iap;
	int                      nIapNumber;
	int m_RequestType;
	
}__struct_download_operator_data;

struct __struct_download_operator_data;
typedef struct __struct_download_operator_data    download_operator_data_t;
typedef struct __struct_download_operator_data *  download_operator_data_ptr_t;


COMM_INTERFACE_( ICxDownOperator, 1, ICommO )
{
	__COMM_INHERIT_FROM( ICommO )
	
	COMM_METHOD( Start)		       ( COMM_REF(ICommO) thiz );
	COMM_METHOD( StartNextTask)	   ( COMM_REF(ICommO) thiz );
	COMM_METHOD( StartCurrent)		   ( COMM_REF(ICommO) thiz );
	COMM_METHOD( Pause )			   ( COMM_REF(ICommO) thiz );
	COMM_METHOD( Stop )	           ( COMM_REF(ICommO) thiz );
	COMM_METHOD( DataProcess)		   ( COMM_REF(ICommO) thiz, IN void* data );
	COMM_METHOD( Complete)		       ( COMM_REF(ICommO) thiz );
	COMM_METHOD( SetObserver )	       ( COMM_REF(ICommO) thiz, IN COMM_REF(ICommO) obsvr );
	COMM_METHOD( NotifyGetLinkEvent)  ( COMM_REF(ICommO) thiz,  IN int status );
	COMM_METHOD( NotifyStatus)        ( COMM_REF(ICommO) thiz,  IN int status );
	COMM_METHOD( NotifyProgress)      (COMM_REF(ICommO) thiz, IN int);
	COMM_METHOD( NotifyTotalProgress) (COMM_REF(ICommO) thiz, IN int aReceived, IN int aTotalSize);	
	COMM_METHOD( CloseDownloadFile) ( COMM_REF(ICommO) thiz );
	COMM_METHOD( SetIap)        ( COMM_REF(ICommO) thiz,  IN int iap);
};

/*************************************************************************
* DECLARATION
*************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif	// #if defined(__cplusplus)

	COMM_IMPORT COMM_REF(ICommO)	comm_cx_create_download_operator( void * reserved );

#if defined(__cplusplus)
}
#endif	// #if defined(__cplusplus)

#define IComm_cx_DownOperator_VTBL( p )				COMM_GET_VTBL_(p, ICxDownOperator)
#define IComm_cx_DownOperator_CALL( p, a )				IComm_cx_DownOperator_VTBL( p )->a
#define IComm_cx_DownOperator_Start( p )	            IComm_cx_DownOperator_CALL( p, Start )( p )
#define IComm_cx_DownOperator_StartNextTask( p )	IComm_cx_DownOperator_CALL( p, StartNextTask )( p )
#define IComm_cx_DownOperator_StartCurrent( p )	    IComm_cx_DownOperator_CALL( p, StartCurrent )( p )
#define IComm_cx_DownOperator_Pause( p )	            IComm_cx_DownOperator_CALL( p, Pause )( p )
#define IComm_cx_DownOperator_Stop( p )	            IComm_cx_DownOperator_CALL( p, Stop )( p )
#define IComm_cx_DownOperator_DataProcess( p,a )	    IComm_cx_DownOperator_CALL( p, DataProcess )( p,a )
#define IComm_cx_DownOperator_Complete( p )	        IComm_cx_DownOperator_CALL( p, Complete )( p )
#define IComm_cx_DownOperator_SetObserver( p, a)	    IComm_cx_DownOperator_CALL( p, SetObserver )( p,a )
#define IComm_cx_DownOperator_NotifyGetLinkEvent( p, a )    IComm_cx_DownOperator_CALL( p, NotifyGetLinkEvent )( p, a )
#define IComm_cx_DownOperator_NotifyStatus( p, a )           IComm_cx_DownOperator_CALL( p, NotifyStatus )( p, a )
#define IComm_cx_DownOperator_NotifyProgress( p, a )         IComm_cx_DownOperator_CALL( p, NotifyProgress )( p, a )
#define IComm_cx_DownOperator_NotifyTotalProgress( p, a ,b)  IComm_cx_DownOperator_CALL( p, NotifyTotalProgress )( p, a ,b )

#define IComm_cx_DownOperator_CloseDownloadFile( p )	IComm_cx_DownOperator_CALL( p, CloseDownloadFile )( p )
#define IComm_cx_DownOperator_SetIap( p, a )           IComm_cx_DownOperator_CALL( p, SetIap )( p, a )

#endif /* DOWNLOAD_OPERATOR_H_ */
